package com.zm.task;

import com.zm.util.Ssh2Pool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ZM on 2022/2/25 13:44
 */
@Component
public class TaskPools {
    Ssh2Pool ssh2Pool = Ssh2Pool.getInstance();
    Logger log = LoggerFactory.getLogger(TaskPools.class);
    //springframework自带的定时器，也可以使用Quartz来实现
    @Scheduled(cron = "0 0 0,6,9,12,15,18,21 * * ? ")//每天的24点、6点、9点、12点、15点、18点、21点执行一次
    public void NarrowSshPoolsTask(){
        try {
            Date now=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            log.info(sdf.format(now)+"======定时器执行缩小Ssh连接池======");
            ssh2Pool.narrowPools();
        } catch (Exception e) {
            log.error("定时器执行缩小Ssh连接池异常{}"+e.getMessage());
        }
    }
}
